package com.o2o.controller.local;

import com.o2o.dto.LocalAuthExecution;
import com.o2o.entity.LocalAuth;
import com.o2o.entity.PersonInfo;
import com.o2o.enums.LocalAuthStateEnum;
import com.o2o.exceptions.LocalAuthOperationException;
import com.o2o.service.LocalAuthService;
import com.o2o.utils.CodeUtil;
import com.o2o.utils.HttpServletRequestUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * package_name:com.o2o.controller.local
 * Author:徐亚远
 * Date:2019/12/26 12:36
 * 项目名:o2o
 * Desription:
 **/
@Controller
@RequestMapping(value = "local",method = {RequestMethod.GET,RequestMethod.POST})
public class LocalAuthController {
    @Autowired
    private LocalAuthService localAuthService;

    @RequestMapping(value = "/bindlocalauth",method = RequestMethod.GET)
    @ResponseBody
    /*
    * 将用户信息与平台账户绑定
    * @param request
    * @return
    * */
    private Map<String,Object> bindLocalAuth(HttpServletRequest request){
        Map<String,Object> modelMap = new HashMap<>();
        //验证码的校验
        if (!CodeUtil.checkVerifyCode(request)){
            modelMap.put("success",false );
            modelMap.put("errMsg","输入了错误的验证码" );
            return modelMap;
        }
        //获取输入的账户
        String username = HttpServletRequestUtil.getString(request,"username" );
        //获取输入的密码
        String password = HttpServletRequestUtil.getString(request,"password" );
        //从session中获取当前用户信息(用户一旦通过通过微信登陆之后,便能获取到用户信息)
        PersonInfo user = (PersonInfo) request.getSession().getAttribute("user");
        //非空判断,要求账户密码以及当前的用户session非空
        if (username!=null && password !=null && user!=null &&user.getUserId()!=null){
             //创建localAuth对象并赋值
            LocalAuth localAuth = new LocalAuth();
            localAuth.setUsername(username);
            localAuth.setPassword(password);
            localAuth.setPersonInfo(user);
            //绑定账号
            LocalAuthExecution le = localAuthService.bindLocalAuth(localAuth);
            if (le.getState() == LocalAuthStateEnum.SUCCESS.getState()){
                modelMap.put("success",true );
            }else {
                modelMap.put("success",false );
                modelMap.put("errMsg",le.getStateInfo() );
            }
        }else {
            modelMap.put("success",false );
            modelMap.put("errMsg","用户名和密码均不能为空" );
        }
        return modelMap;
    }
    @RequestMapping(value = "/changelocalpwd",method = RequestMethod.POST)
    @ResponseBody
    /*
    * 修改密码
    * @param request
    * @return
    * */
    private Map<String,Object> changeLocalPwd(HttpServletRequest request){
        Map<String,Object> modelMap = new HashMap<>();
        //验证码的校验
        if (!CodeUtil.checkVerifyCode(request)){
            modelMap.put("success",false );
            modelMap.put("errMsg","输入了错误的验证码" );
            return modelMap;
        }
        //获取账户
        String username = HttpServletRequestUtil.getString(request,"username" );
        //获取原密码
        String password = HttpServletRequestUtil.getString(request,"password");
        //获取新密码
        String newPassword = HttpServletRequestUtil.getString(request,"newPassword" );
         //从session中获取当用户信息(用户一旦通过微信登录之后,便能获取到用户信息)
        PersonInfo user = (PersonInfo) request.getSession().getAttribute("user");
        //非空判断,要求账号新旧密码以及当前的用户session非空,且新旧密码不相同
        if (username !=null && password !=null && newPassword !=null
                && user!=null && user.getUserId()!=null && !password.equals(newPassword)){
            try {
                //查看原先的账号,看看与输入的账号是否一致,不一致则认为是非法操作
                LocalAuth localAuth = localAuthService.getLocalAuthByUserId(user.getUserId());
                if (localAuth == null || !localAuth.getUsername().equals(username)){
                    //不一致则直接退出
                    modelMap.put("success",false );
                    modelMap.put("errMsg","输入的账号非本次登录的账号" );
                    return modelMap;
                }
                //修改平台账号的用户密码
                LocalAuthExecution le = localAuthService.modifyLocalAuth(user.getUserId(),username ,password ,
                        newPassword , new Date());
                if (le.getState()==LocalAuthStateEnum.SUCCESS.getState()){
                    modelMap.put("success",true );
                }else {
                    modelMap.put("success",false );
                    modelMap.put("errMsg",le.getStateInfo() );
                }
            }catch (LocalAuthOperationException e){
                modelMap.put("success",false );
                modelMap.put("errMsg",e.toString() );
                return modelMap;
            }
        }else {
            modelMap.put("success",false );
            modelMap.put("errMsg","请输入密码" );
        }
        return modelMap;
    }
    @RequestMapping(value = "/logincheck",method = RequestMethod.POST)
    @ResponseBody
    private Map<String,Object> loginCheck(HttpServletRequest request){
        Map<String,Object> modelMap = new HashMap<>();
        //获取是否需要进行验证码校验的标识符
        boolean needVerify = HttpServletRequestUtil.getBoolean(request,"needVerify" );
        if (needVerify && !CodeUtil.checkVerifyCode(request)){
            modelMap.put("success",false );
            modelMap.put("errMsg","输入错误的验证码" );
            return modelMap;
        }
        //获取输入的账号
        String username = HttpServletRequestUtil.getString(request,"username" );
        //获取输入的密码
        String password = HttpServletRequestUtil.getString(request,"password" );
        //非空校验
        if (username!=null&&password!=null){
            //传入账号和密码去获取平台的账户信息
            LocalAuth localAuth = (LocalAuth) localAuthService.getLocalAuthByUsernameAndpwd(username,password );
       if (localAuth!=null){
           //若能获取账号信息则登陆成功
           modelMap.put("success",true );
           //同时在session里设置用户信息
           request.getSession().setAttribute("user",localAuth.getPersonInfo());
       }else {
           modelMap.put("success",false );
           modelMap.put("errMsg","用户名和密码错误" );
       }
        }else {
            modelMap.put("success",false );
            modelMap.put("errMsg","用户名和密码均不能为空" );
        }
        return modelMap;
    }
    @RequestMapping(value = "/loginout",method = RequestMethod.POST)
    @ResponseBody
    /*
    * 当用户点击退出按钮的时候注销session
    * @param request
    * @return
    * @throws IOException
    * */
    private Map<String,Object> loginOut(HttpServletRequest request){
        Map<String,Object> modelMap = new HashMap<>();
        //将用户session置为空
        request.getSession().setAttribute("user",null );
        modelMap.put("success",true );
        return modelMap;
    }
}
